﻿using DataServer.ApplicationLib.Entities;
using System.Data;
using System.Diagnostics.CodeAnalysis;

namespace DataServer.ApplicationLib.Rules
{
    public class RuleContext
    {
        public bool IsValid
        {
            get
            {
                bool retVal = false;

                if (null == this.ConfigInfo)
                {
                    throw new ArgumentNullException("不是有效的配置编码");
                }

                InitStoreCheckMap();
                if (this.StoreCheckColMap?.Count == 0)
                {
                    throw new InvalidOperationException("缺省存储与校验之间的列映射");
                }

                if (null == ConfigFieldInfos)
                {
                    throw new ArgumentNullException("ConfigFieldInfos");
                }

                if (null == TargetData || TargetData.Rows.Count == 0)
                {
                    throw new ArgumentNullException("TargetData", "需要校验的数据为空");
                }

                retVal = true;
                return retVal;
            }
        }

        private void InitStoreCheckMap()
        {
            StoreCheckColMap = new List<StoreCheckCol>();
            if (ConfigFieldInfos != null)
            {
                foreach (var item in ConfigFieldInfos)
                {
                    StoreCheckColMap.Add(new StoreCheckCol(item.FieldCode, item.ValueColumnMap));
                }
            }
        }

        [NotNull]
        public DataTable? TargetData { get; set; }

        public Guid? RequestId { get; set; }

        [NotNull]
        public DataConfigEntity? ConfigInfo { get; set; }

        [NotNull]
        public IEnumerable<ConfigFieldEntity>? ConfigFieldInfos { get; set; }

        public IEnumerable<ConfigRuleEntity>? ConfigRuleInfos { get; set; }

        public IList<string> DbUpdatedColList { get; set; } = new List<string>();

        public IList<string> MemoryUpdatedColList { get; set; } = new List<string>();

        [NotNull]
        public IList<StoreCheckCol>? StoreCheckColMap { get; set; }

        [NotNull]
        public IServiceProvider? ServiceProvider { get; set; }

        public string? CurrentUserName { get; set; }

        public UserOperationType CurUserOpType { get;  set; }
    }
}